package com.qidianit.mes.pro.controller;

import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pro.mapper.WorkFlowProcessMapper;
import com.qidianit.mes.pro.service.WorkFlowProcessService;
import com.qidianit.mes.pro.vo.WorkflowProcessRelation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description:
 * @Author: shanzhe
 * @Date: 2025/9/18 - 19:12
 */
@RestController
@RequestMapping("/workflowProcess")
public class WorkFlowProcessController {
    @Autowired
    private WorkFlowProcessService workFlowProcessService;


    @GetMapping("/page")
    public Result<PageInfo<WorkflowProcessRelation>> queryProcessCompositionsByWorkflowIdWithPaging(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                                                                    @RequestParam(value = "size",defaultValue = "10") Integer sieze,
                                                                                                    @RequestParam(value = "workflowId",required = true) Integer workflowId){

        return Result.success(workFlowProcessService.queryProcessCompositionsByWorkflowIdWithPaging(page,sieze,workflowId));
    }

    @PostMapping("/add")
    public Result addWorkflowProcessRelation(@RequestBody WorkflowProcessRelation workflowProcessRelation) {
        workflowProcessRelation.setCreateTime(LocalDateTime.now());
        workflowProcessRelation.setCreateBy("1001");
        int result = workFlowProcessService.addWorkflowProcessRelation(workflowProcessRelation);
        if (result == -1)
        {
            return Result.error("工序序号["+workflowProcessRelation.getProcessId()+"]已经存在");
        }
        if (result == -2)
        {
            return Result.error("工序"+workflowProcessRelation.getStepOrder()+"已经存在");
        }
        return result == 1?
                Result.success():Result.error("操作失败");
    }

    @GetMapping("/{id}")
    public Result<WorkflowProcessRelation> getWorkflowProcessRelationById(@PathVariable("id") Integer id) {
        return Result.success(workFlowProcessService.getWorkflowProcessRelationById(id));
    }

    @PutMapping("/update")
    public Result updateWorkflowProcessRelation(@RequestBody WorkflowProcessRelation workflowProcessRelation) {
        workflowProcessRelation.setUpdateTime(LocalDateTime.now());
        workflowProcessRelation.setUpdateBy("1002");
        int result = workFlowProcessService.updateWorkflowProcessRelation(workflowProcessRelation);
        if (result == -1)
        {
            return Result.error("工序序号["+workflowProcessRelation.getProcessId()+"]已经存在");
        }
        if (result == -2)
        {
            return Result.error("工序"+workflowProcessRelation.getStepOrder()+"已经存在");
        }
        return result == 1?
                Result.success():Result.error("操作失败");
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteWorkflowProcessRelationById(@PathVariable("id") Integer id) {
        return workFlowProcessService.deleteWorkflowProcessRelationById(id) == 1?Result.success():Result.error("操作失败");
    }

    @DeleteMapping("/batchDelete")
    public Result batchDeleteWorkflowProcessRelation(@RequestParam List<Integer> id) {
        int result = workFlowProcessService.deleteWorkflowProcessRelationByIds(id);
        return result == 1?Result.success():result == -1?Result.error("删除部分"):Result.error("操作失败");
    }
}
